package com.baselibrary.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.ViewPager;

import com.baselibrary.BaseConstant;
import com.baselibrary.Constants;
import com.baselibrary.R;
import com.baselibrary.adapter.LocusImagePageAdapterNew;
import com.baselibrary.databinding.ViewLocusImageBinding;
import com.baselibrary.router.ActivityRouter;
import com.baselibrary.utils.ARouterUtils;
import com.baselibrary.utils.DoubleClickHelper;
import com.baselibrary.utils.ScreenUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @author:hanke 2022/4/27
 * desc:
 */
public class LocusImageView extends RelativeLayout {
    private ViewLocusImageBinding mBinding;
    private OnDoubleClickListener onDoubleClickListener;

    public LocusImageView(Context context) {
        super(context);
    }

    public LocusImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LocusImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public LocusImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setData(Context context, List<String> imgUrls, String id) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_locus_image, this);
        view.setTag("layout/view_locus_image_0");
        mBinding = DataBindingUtil.bind(view);
        if (imgUrls.size() > 0) {
            mBinding.pivVp.setVisibility(imgUrls.size() > 1 ? View.VISIBLE : View.GONE);
            mBinding.pivVp.setSumCount(imgUrls.size());
            mBinding.tvLocusImgcount.setText(1 + "/" + imgUrls.size());
            int viewPagerHeight = setViewpagerHeight(context, imgUrls, mBinding.vpImages);
            LocusImagePageAdapterNew mLocusPageAdapter = new LocusImagePageAdapterNew(imgUrls, viewPagerHeight, (Activity) context);
            mLocusPageAdapter.setOnDClickListener(new LocusImagePageAdapterNew.OnDClickListener() {
                @Override
                public void onDoubleClick() {
                    if (onDoubleClickListener != null)
                        onDoubleClickListener.onClick();
                }

                @Override
                public void onSingleClick() {
                    if (!DoubleClickHelper.isOnDoubleClick()) {
                        goImageDetail(id);
                    }
                }
            });
            mBinding.vpImages.setAdapter(mLocusPageAdapter);
            mBinding.vpImages.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageScrolled(int i, float v, int i1) {

                }

                @Override
                public void onPageSelected(int i) {
                    int onPageSelectedPos = 0;
                    if (i != 0 && imgUrls.size() > 0) {
                        onPageSelectedPos = i % imgUrls.size();
                    }
                    mBinding.tvLocusImgcount.setText((onPageSelectedPos + 1) + "/" + imgUrls.size());
                    mBinding.pivVp.setCurrentShowView(i % imgUrls.size());
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }

    private int setViewpagerHeight(Context activity, List<String> data, ViewPager viewPager) {
        float viewHeight = Float.parseFloat(ScreenUtils.getVideoHight(data.get(0)));
        float viewWidth = Float.parseFloat(ScreenUtils.getVideoWidth(data.get(0)));
        float scale = Constants.FOLLOW_PICTURE_SCALE;//默认显示宽高比
        float imageRatio = viewWidth / viewHeight;//图片宽高比
        float mScreenWidth = ScreenUtils.getScreenWidth(activity) - ScreenUtils.dip2px(activity, 28);//
        float newW = 0;
        float newH = 0;
        if (imageRatio >= scale) {
            newW = mScreenWidth;
            newH = newW / imageRatio;
        } else {
            newH = mScreenWidth / scale;
            newW = newH * imageRatio;
        }
        LayoutParams layoutParams = (LayoutParams) viewPager.getLayoutParams();
        layoutParams.height = (int) newH;
        viewPager.setLayoutParams(layoutParams);
        return (int) newH;
    }

    public void goImageDetail(String id) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", id);
        hashMap.put("source", BaseConstant.POINT_DYNAMIC_FOLLOW);
        ARouterUtils.jump(ActivityRouter.ACTIVITY.IMAGE_DETAIL, hashMap);
    }

    public void setOnDoubleClickListener(OnDoubleClickListener onDoubleClickListener) {
        this.onDoubleClickListener = onDoubleClickListener;
    }

    public interface OnDoubleClickListener {
        void onClick();
    }
}
